/**
 * 表单
 * 
 * @class Ext.lams.StudentInfoForm
 * @extends Ext.form.FormPanel
 */
ArticleInfoForm = Ext.extend(Ext.form.FormPanel, {
	constructor : function(_cfg) {
		Ext.apply(this, _cfg || {});
		ArticleInfoForm.superclass.constructor.call(this, {
					layout : "form",
					frame : true,
					fileUpload : true,
					defaults:{anchor:'90%',labelWidth : 65},
					items:[{
						xtype:'textfield',
						fieldLabel : '作品名称',
						allowBlank : false,
						blankText : "作品名称不能为空",
						name : 'ARTICLE_NAME'
					},{
						xtype:'textfield',
						fieldLabel : '作者',
						name :'AUTHOR'
					},{
						xtype:'ckeditor',
						fieldLabel : '作品内容',
						name:'CONTENT'
					},{
						xtype:'hidden',
						name:'ARTICLE_ID'
					}]
				});
	},
	initComponent : function() {
		ArticleInfoForm.superclass.initComponent.call(this);
	},
	getValue : function() {
		if (this.getForm().isValid())
			return new Ext.data.Record(this.getForm().getValues());
		else
			throw Error("表单填写出错");
	},
	setValues : function(_r) {
		//this.getForm().loadRecord(_r);
		this.find('name','ARTICLE_NAME')[0].setValue(_r.data['ARTICLE_NAME']);
		this.find('name','CONTENT')[0].setValue(_r.data['CONTENT']);
		this.find('name','ARTICLE_ID')[0].setValue(_r.data['ARTICLE_ID']);
		this.find('name','AUTHOR')[0].setValue(_r.data['AUTHOR']);
	},
	clear : function() {
		this.getForm().reset();
	},
	submit : function(fn) {
		if (this.getForm().isValid()) {
			this.getForm().submit({
						// clientValidation: true,
						waitMsg : "正在保存数据...",
						method : "POST",
						// isUpload:true,
						url : "addOrUpdateArticleAction.action",
						success : function(form, action) {
							fn(form, action);
						},
						faild : function(form, action) {
							fn(form, action);
						}
					});
		}
	}
});

/**
 * 列表
 * 
 * @class Ext.lams.StudentGrid
 * @extends Ext.grid.GridPanel
 */
ArtcileGrid = Ext.extend(Ext.grid.GridPanel, {
			constructor : function(_cfg) {
				if (!_cfg)
					_cfg = {};
				Ext.apply(this, _cfg);
				ArtcileGrid.superclass.constructor.call(this, {
							frame : true,
							loadMask : {
								msg : '正在载入数据,请稍等...'
							},
							listeners : {
								rowdblclick : function(g) {
									var r = g.getSelectionModel().getSelected();
									this.fireEvent("rowdb", this, r);
									// alert(d.data);
								}
							}
						});
				this.addEvents("rowdb");
				this.addEvents("edit");
				this.addEvents("delete");
			},
			initComponent : function() {
				var me = this;
				var _store = new Ext.data.JsonStore({
							proxy : new Ext.data.HttpProxy({
										url : "queryArtcileAction.action",
										method : "GET",
										baseParams : {}
									}),
							totalProperty : "totalCount",
							root : "data",
							fields : [{
										name : "AUTHOR"
									},{
										name : "ARTICLE_ID"
									}, {
										name : "ARTICLE_NAME"
									}, {
										name : "CONTENT"
									}]
						});
				var _sm = new Ext.grid.RowSelectionModel({
							singleSelect : true
						});
				var _cm = new Ext.grid.ColumnModel({
					columns:[new Ext.grid.RowNumberer(),
						{
							header : "作者",
							dataIndex : "AUTHOR"
						},{
							header : "作品编号",
							hidden : true,
							dataIndex : "ARTICLE_ID"
						}, {
							header : "作品名称",
							dataIndex : "ARTICLE_NAME"
						}, {
							header : "内容",
							dataIndex : "CONTENT",
							width:600,
							renderer : function(v,d){
								var a = v.toString();
								return a.substring(0,30)+"...";
								//return value.toString().substring(0,30) + "...";
							}
						}, {
							header : "",
							width : 30,
							renderer : function(v, d, r, ridx, cidx, s) {
								return me.edit_render(r);
							}
						}, {
							header : "",
							width : 30,
							renderer : function(v, d, r, ridx, cidx, s) {
								return me.delete_render(r);
							}
						}],
					defaults:{
						resizable: false,
						sortable: false,
						menuDisabled: true
					}
				});

				var bbar = new Ext.lams.comp.PagingToolbar({
							store : _store
						});
				Ext.apply(this, {
							store : _store,
							cm : _cm,
							sm : _sm,
							bbar : bbar
						});
				ArtcileGrid.superclass.initComponent.call(this);
			},
			getSelectRow : function() {
				var sel = this.getSelectionModel();
				if (sel.getCount() == 0)
					throw new Error("未选中某条记录");
				return sel.getSelected();
			},
			addStudenRow : function(_r) {
				this.getStore().add(_r);
			},
			deleteStudentRow : function() {
				try {
					var sel = this.getSelectRow();
					var grid = this;
					Ext.Ajax.request({
								url : '',
								method : 'POST',
								params : {
									
					            },
								success : function() {
									grid.getStore().remove(sel);
								}
							});
				} catch (_err) {
					// Ext.Msg.alert("Error", _err.message);
					H.Msg.error(_err.message);
				}
			},
			modifySelectRow : function(_r) {
				try {
					var _sr = this.getSelectRow();
					var _data = _sr.data;
					for (var _i in _data) {
						_sr.set(_i, _r.get(_i));
					}
					_sr.commit();
				} catch (_err) {
					// Ext.Msg.alert("Error", _err.message);
					H.Msg.error(_err.message);
				}
			},
			edit_render : function(recode) {
				var me = this;
				var contentId = Ext.id();
				var btn = createGridButton.defer(1, this, [contentId]);
				function createGridButton() {
					return new Ext.Button({
								iconCls : 'silk-user-edit',
								// width:16,
								tooltip : '编辑',
								handler : function() {
									me.fireEvent('edit', recode, me);
								}
							}).render(document.body, contentId);
				}
				return ('<div id="' + contentId + '"/>');
			},
			delete_render : function(recode) {
				var me = this;
				var contentId = Ext.id();
				var btn = createGridButton.defer(1, this, [contentId]);
				function createGridButton() {
					return new Ext.Button({
								iconCls : 'silk-user-delete',
								tooltip : '删除',
								handler : function() {
									me.fireEvent('delete', recode, me);
								}
							}).render(document.body, contentId);
				}
				return ('<div id="' + contentId + '"/>');
			}
		});
/**
 * 主界面
 * 
 * @class Ext.lams.StudentManagerPanel
 * @extends Ext.Panel
 */
ArticleManagerPanel = Ext.extend(Ext.Panel, {
	grid : {},
	constructor : function(_cfg) {
		if (!_cfg)
			_cfg = {};
		Ext.apply(this, _cfg);
		ArticleManagerPanel.superclass.constructor.call(this);
		this.grid.on("rowdb", this.onRowdbClick, this);
		this.grid.on("edit", this.onEditHandler, this);
		this.grid.on("delete", this.onDeleteHandler, this);
	},
	initComponent : function() {
		var me = this;
		var h = new Ext.form.FormPanel({
			region : "north",
			height : 80,
			split : true,
			frame : true,
			collapseMode : 'mini',
			layout : "form",
			items:[{
				layout:'hbox',
				anchor : '100%',
				baseCls : 'x-plain',
				layoutConfig : {
					padding : 5
				},
				defaults : {
					margins : '0 15 0 0'
				},
				items:[{
					layout:'form',
					items:[{
						xtype:'textfield',
						fieldLabel:'作者',
						name:'AUTHOR'
					}]
					
				},{
					layout:'form',
					items:[{
						xtype:'textfield',
						fieldLabel:'作品名称',
						name:'ARTICLE_NAME'
					}]
				}]
			}],
			buttons : [{
					xtype : "btn_query",
					handler : function() {
						var btn = this;
						btn.disable();
						var _params = me.form.getForm().getValues();
						H.Msg.info(_params.author);
						var ds = me.grid.getStore();
						var pagingbar = me.grid.getBottomToolbar();
						ds.baseParams = _params;
						ds.load({
									params : {
										start : 0,
										limit : pagingbar.pageSize
									},
									callback : function(a, b) {
										btn.enable();
									}
								});
					}
				},{
					xtype:'btn_new',
					handler : function(){
						me.editWin.show();
					}
				}]
		});
		this.form = h;

		var center = new ArtcileGrid({
					region : "center"
				});
		this.grid = center;
		var sf = new ArticleInfoForm();
		this.infoForm = sf;

		var win = new Ext.Window({
			width : 800,
			//height:600,
			title : "作品信息",
			form : this.infoForm,
			closeAction : "hide",
			items : [this.infoForm],
			buttons : [{
				xtype : "btn_save",
				handler : function() {
					me.infoForm.submit(function(_form, action) {
						if (action.result['success']) {
							// Ext.Msg.alert('提示', action.result['message']);
							H.Msg.info(action.result['message'], function() {
								me.editWin.form.clear();
								me.editWin.hide();
								me.grid.getStore().reload();
							});
						}
					});
				}
			}, {
				xtype : "btn_cancel",
				handler : function() {
					me.editWin.form.clear();
					me.editWin.hide();
				}
			}]
		});
		this.editWin = win;
		Ext.apply(this, {
					layout : 'border',
					frame : true,
					items : [h, center]
				});
		ArticleManagerPanel.superclass.initComponent.call(this);
	},
	onRowdbClick : function() {
		try {
			// if(!_r)_r = this.grid.getSelectRow()
		//	this.editWin.form.clear();
			this.editWin.show();
			this.editWin.form.setValues(this.grid.getSelectRow());

		} catch (_err) {
			// Ext.Msg.alert("Error", _err.message);
			H.Msg.error(_err.message);
		}
	},
	onEditHandler : function(_r) {
	//	this.editWin.form.clear();
		this.editWin.show();
		this.editWin.form.setValues(_r);
	},
	onDeleteHandler : function(_r) {
		// Ext.Msg.alert(_r.data['STUDENT_NAME']);
		this.grid.getStore().remove(_r);
	}
});